<?php


namespace app\index\controller;

use app\index\model\Banner;
use app\index\model\Crane;
use app\index\model\Plan as PlanModel;
use app\index\model\Record as RecordModel;
use app\index\model\News as NewsModel;
use app\index\model\GoodsCat;
use app\index\model\Process;
use app\index\model\ProductionStage;
use app\index\model\SegmentType;
use think\admin\Controller;
use think\facade\Db;

class Record extends Base
{


    /**
     * @name:开始记录首页
     * @author:Sam
     * @date:2024-10-24
     * @param:[string] param [desc]
    */
    public function  index(){
        $car_id =  request()->get('car_id/d',0);
        $where  = [];
        if($car_id > 0) $where[] = ['crane_id','=',$car_id];
        $this->carne_list       = Crane::select();
        $this->list = PlanModel::where($where)->with(['crane','segment','stage'])->order('id desc')->select();
        $this->car_id = $car_id;
        return $this->fetch();
    }

    //新增记录页面
    public function record_add($id){
        if(!$detail = PlanModel::with(['crane','segment','stage'])->find($id)) return $this->error('数据不存在');
        $this->process = Process::order('sort asc')->select();
        $this->detail = $detail;
        $this->plan_id  = $id;
        $this->record_list = RecordModel::where('plan_id',$id)->with(['process'])->order('id asc')->select();
        return $this->fetch();
    }





    //开始记录
    public function start_record(){
        if($this->app->request->isPost()){
            $post_data = $this->_vali([
                'plan_id.require'               => '计划不能为空!',
                'process_id.require'            => '工序不得为空!',
            ]);
            if(!$plan = PlanModel::find($post_data['plan_id'])) return $this->error('计划不存在');
            $post_data['start_time'] = date('Y-m-d H:i:s');
            if(!$process = Process::find($post_data['process_id'])){
                return $this->error('工序不存在');
            }
               //查找上一道工序
            $last_process = Process::where('sort','<',$process->sort)->order('id desc')->find();
            if($last_process){
                //查找上一道工序流程
                $last_process_record = RecordModel::where('plan_id',$post_data['plan_id'])->where('process_id',$last_process->id)->find();
                if(!$last_process_record || $last_process_record->status ==0){
                    return $this->error('上一道工序: '.$last_process->name.' 未完成！');
                }
            }
            if(RecordModel::where('plan_id',$post_data['plan_id'])->where('process_id',$post_data['process_id'])->find()){
                return $this->error('该工序已添加');
            }
            $post_data['create_time'] = date('Y-m-d H:i:s');
            $post_data['update_time'] = date('Y-m-d H:i:s');
            $post_data['start_time']  = date('Y-m-d H:i:s');
            $post_data['status']      = 0;
            if(RecordModel::create($post_data)) return $this->success('操作成功');
            return $this->error('操作失败');
        }else{
            $this->segment_type_list = SegmentType::select();
            $this->production_stage_list = ProductionStage::select();
            $this->carne_list       = Crane::select();
            return $this->fetch();
        }
    }

   //跳过工序页面
   public function record_jump(){
    if($this->app->request->isPost()){
        $post_data = $this->_vali([
            'record_id.require'             => '工序不能为空!',
        ]);
        if(!$record = RecordModel::find($post_data['record_id'])) return $this->error('记录不存在');
            if(!$plan = PlanModel::find($record['plan_id'])) return $this->error('计划不存在');
            if(!$process = Process::find($record['process_id'])){
                return $this->error('工序不存在');
            }
            if($record->status !=0)  return $this->error('计划已完成');
  
    
            Db::startTrans();
            try {
                $record->status = 2;
                $record->start_time   = null;
                $record->update_time   = date('Y-m-d H:i:s');
                $record->remark       = '已跳过';
                $record->save();
                $next_process = Process::where('sort','>',$process->sort)->find(); //查找下一步
                if(!$next_process){ 
                    if(strtotime($post_data['end_time']) <= strtotime($plan->end_time)){
                        //正常完成
                        $plan->status = 1;
                    }else{
                        //超时完成
                        $plan->status = 2;
                    }
                    //已经是最后一步
                    $plan->complete_time = date('Y-m-d H:i:s');
                    $plan->save();
                }else{
                       //有下一步
                       RecordModel::create([
                        'plan_id'  => $record->plan_id,
                        'process_id' => $next_process->id,
                        'start_time' => date('Y-m-d H:i:s'),
                    ]);
                }
                Db::commit();
            } catch (\Exception $e) {
               Db::rollback();
               return $this->error('操作失败'.$e->getMessage());
            }

             return $this->success('操作成功');
    }else{
        return $this->error('请求异常');
    }
}
    
    //结束工序页面
    public function record_over($id){
        $this->record_id = $id;
        if(!$record = RecordModel::find($id)) return $this->error('记录不存在');
        $this->plan_id = $record->plan_id;
        $this->start_time = $record->start_time;
        return $this->fetch();
    }
    
    //结束工序
    public function record_over_sumbit(){
        if($this->app->request->isPost()){
            $post_data = $this->_vali([
                'record_id.require'             => '工序不能为空!',
                'end_time.require'              => '日期不得为空!',
            ]);
            if(!$record = RecordModel::find($post_data['record_id'])) return $this->error('记录不存在');
            if(!$plan = PlanModel::find($record['plan_id'])) return $this->error('计划不存在');
            if(!$process = Process::find($record['process_id'])){
                return $this->error('工序不存在');
            }
            if($record->status !=0)  return $this->error('计划已完成');
            if(strtotime($post_data['end_time']) <=  strtotime($record->start_time))  return $this->error('结束日期不得早于开始日期');
            $data['remark']     = input('remark','');
            $data['end_time'] =  $post_data['end_time'];
    
            Db::startTrans();
            try {
                $record->status = 1;
                $record->end_time = $post_data['end_time'];
                $record->update_time   = date('Y-m-d H:i:s');
                $record->remark       = input('remark','');
                $record->save();
                $next_process = Process::where('sort','>',$process->sort)->find(); //查找下一步
                if(!$next_process){
                    if(strtotime($post_data['end_time']) <= strtotime($plan->end_time)){
                        //正常完成
                        $plan->status = 1;
                    }else{
                        //超时完成
                        $plan->status = 2;
                    }
                    //已经是最后一步
                    $plan->complete_time = date('Y-m-d H:i:s');
                    $plan->save();
                }else{
                    //有下一步
                    RecordModel::create([
                        'plan_id'  => $record->plan_id,
                        'process_id' => $next_process->id,
                        'start_time' => date('Y-m-d H:i:s'),
                    ]);
                }
                Db::commit();
            } catch (\Exception $e) {
               Db::rollback();
               return $this->error('操作失败'.$e->getMessage());
            }

             return $this->success('操作成功');
        }else{
             return $this->error('请求异常');
        }
    }


  
}
